package com.lisn.opengl;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import androidx.databinding.DataBindingUtil;
import androidx.databinding.ViewDataBinding;

import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

import com.lisn.opengl.databinding.ActivityTestOpenGLBinding;

public class TestOpenGLActivity extends AppCompatActivity implements View.OnClickListener {

    private static final String TAG = TestOpenGLActivity.class.getSimpleName();
    private ActivityTestOpenGLBinding binding;
    private int CAMERA_OK_CODE = 111;


    public static void startActivity(Context context) {
        context.startActivity(new Intent(context, TestOpenGLActivity.class));
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //setContentView(R.layout.activity_test_open_g_l);

        getPermission();
    }

    private void init() {
        binding = DataBindingUtil.setContentView(this, R.layout.activity_test_open_g_l);
        binding.btn1.setOnClickListener(this);
    }

    private void getPermission() {
        if (Build.VERSION.SDK_INT > 22) {
            if (ContextCompat.checkSelfPermission(TestOpenGLActivity.this,
                    android.Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
                ActivityCompat.requestPermissions(TestOpenGLActivity.this,
                        new String[]{Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE}, CAMERA_OK_CODE);
                Log.i("TestOpenGLActivity", "requestPermissions");
            } else {
                Log.i("TestOpenGLActivity", "已经获取了权限");
                init();
            }
        } else {
            Log.i("TestOpenGLActivity", "这个说明系统版本在6.0之下，不需要动态获取权限。");
            init();
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode == CAMERA_OK_CODE && grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
            init();
        }
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.btn1) {

        }
    }
}